package com.czkevin.guicontrolsapi.controls;

import com.czkevin.guicontrolsapi.GUIControlsAPI;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import com.czkevin.guicontrolsapi.ItemUtils;

public class ItemChooserControl extends AbstractSingleItemControl {

    private final ItemStack icon;
    private String title;
    private String description;

    private final ItemChooseCallback callback;

    private boolean animation_show_icon = false;

    public ItemChooserControl(ItemStack icon, ItemChooseCallback callback) {
        this(icon, null, null, callback);
    }

    public ItemChooserControl(ItemStack icon, String title, String description, ItemChooseCallback callback) {
        if(callback == null) throw new IllegalArgumentException("Icon and callback must NOT be null! ");
        this.icon = icon;
        this.title = title;
        this.description = description;
        this.callback = callback;
    }

    @Override
    protected ItemStack createDisplay() {
        if(icon == null) return null;
        return (title != null && description != null) ?
            ItemUtils.putMeta(icon, title, description) : icon;
    }

    @Override
    public void onRender(boolean force) {
        if(icon != null) {
            animation_show_icon = ! animation_show_icon; // we put it here so onClick() would run without problem!
            if(animation_show_icon) {
                getWindow().setDisplayItemAt(getX(), getY(), icon);
            } else {
                getWindow().setDisplayItemAt(getX(), getY(), null);
            }
        } else {
            if(force) {
                getWindow().setDisplayItemAt(getX(), getY(), null);
            }
        }
    }

    @Override
    public void onClick(InventoryClickEvent inventoryClickEvent, int i, int i1) {
        inventoryClickEvent.setCancelled(true);
        if(inventoryClickEvent.getClick() != ClickType.LEFT) return;
        if(inventoryClickEvent.getCursor() == null) return; // Just to be safe!
        if(icon != null && animation_show_icon && inventoryClickEvent.getAction() != InventoryAction.SWAP_WITH_CURSOR) {
            System.out.println("[Chooser] INVALID 1");
            return;
        } else if((icon == null || !animation_show_icon) && !inventoryClickEvent.getAction().name().startsWith("PLACE")) {
            System.out.println("[Chooser] INVALID 2");
            return;
        }
        ItemStack cursor = inventoryClickEvent.getCursor().clone();
        Bukkit.getScheduler().runTaskLater(GUIControlsAPI.getInstance(), () -> {
            System.out.println("[Chooser] CALLING CALLBACK");
            boolean ok = callback.onChoose(inventoryClickEvent.getWhoClicked(), cursor);
            if(ok) inventoryClickEvent.getWhoClicked().setItemOnCursor(null);
        }, 1L);
    }

    @Override
    public int getWidth() {
        return 1;
    }

    @Override
    public int getHeight() {
        return 1;
    }

    public interface ItemChooseCallback {
        /**
         * Called when an item stack is choosed.
         * @param h
         * @param i
         * @return true to clear the cursor
         */
        boolean onChoose(HumanEntity h, ItemStack i);
    }
}
